﻿Imports Microsoft.VisualBasic
Imports Google.GData.Calendar

Namespace DGZfP.EventPublisher


    Public Class GoogleCalendarController


        Public Function GetService(ByVal name As String, ByVal pwd As String) As CalendarService
            Dim key As String = String.Format("{0}:{1}", name, pwd)

            Dim o As Object = DataCache.GetCache(key)
            If o Is Nothing Then
                Dim myService As New CalendarService("DotNetNuke Event Publisher")
                myService.setUserCredentials(name, pwd)
                DataCache.SetCache(key, myService)
                Return myService
            Else
                Return CType(o, CalendarService)
            End If
           
        End Function

        Public Function GetCalendars(ByVal name As String, ByVal pwd As String) As Generic.IDictionary(Of String, String)
            Dim dic As New Generic.Dictionary(Of String, String)

            Dim query As New CalendarQuery()
            query.Uri = New Uri("http://www.google.com/calendar/feeds/default/owncalendars/full")
            Dim resultFeed As CalendarFeed = GetService(name, pwd).Query(query)

            For Each entry As CalendarEntry In resultFeed.Entries
                Dim id As String = entry.Id.AbsoluteUri.Substring(entry.Id.AbsoluteUri.LastIndexOf("/") + 1)
                dic.Add(id, entry.Title.Text)
            Next

            Return dic

        End Function

    End Class
End Namespace
